package com.twu.biblioteca;

import com.twu.biblioteca.menu.Menu;
import com.twu.biblioteca.menu.MenuOption;

import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class BibliotecaApp {

    private final String WELCOME_MESSAGE = "Welcome to Biblioteca!";

    private final PrintStream console;
    private final InputStream inputStream;
    private final Library library;
    private final Menu menu;

    public BibliotecaApp(PrintStream console, InputStream inputStream) {
        this.console = console;
        this.inputStream = inputStream;
        this.library = new Library();
        this.menu = new Menu(library, console, inputStream);
    }

    public void printWelcomeMessage() {
        this.console.println(this.WELCOME_MESSAGE);
    }

    public Library getLibrary() {
        return library;
    }

    public Menu getMenu() {
        return menu;
    }

    public void run() {
        Scanner scanner = new Scanner(inputStream);

        while (scanner.hasNextLine()) {
            String input = scanner.nextLine();
            MenuOption selected = menu.getOptionFromInput(input);
            if (selected != null)
                menu.selectOption(selected);
        }
    }

    public static void main(String[] args) {
        BibliotecaApp app = new BibliotecaApp(System.out, System.in);
        app.getLibrary().getBooks().add(new Book("Test", "Author", "2014"));
        app.printWelcomeMessage();
        app.menu.printOptions();
        app.run();
    }
}
